package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.mysema.query.BooleanBuilder;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class ProjectReportService{

	@Autowired
	private ProjectReportDao projectReportDao;
	
	private QProjectReport $ = QProjectReport.projectReport;
	
	@Autowired
	public void setProjectReportDao(ProjectReportDao projectReportDao) {
		this.projectReportDao = projectReportDao;
	}
	
	public List<ProjectReport> findAll(){
		return ListUtil.toList(projectReportDao.findAll());
	}
				
	public List<ProjectReport> findAll(
	Long projectId,
	Long peopleId,
	String workProgress,
	String workPlan,
	String issueDescription,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(IdUtil.isNotEmpty(projectId)){
		condition.and($.project.id.eq(projectId));
	}
	
	if(IdUtil.isNotEmpty(peopleId)){
		condition.and($.people.id.eq(peopleId));
	}
	
	if(StringUtil.isNotEmpty(workProgress)){
		condition.and($.workProgress.contains(workProgress));
	}
	
	if(StringUtil.isNotEmpty(workPlan)){
		condition.and($.workPlan.contains(workPlan));
	}
	
	if(StringUtil.isNotEmpty(issueDescription)){
		condition.and($.issueDescription.contains(issueDescription));
	}
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(projectReportDao.findAll(condition));
	}		

	public Page<ProjectReport> findAll(
			Long projectId,
			Long peopleId,
			String workProgress,
			String workPlan,
			String issueDescription,
			String remark,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(IdUtil.isNotEmpty(projectId)){
				condition.and($.project.id.eq(projectId));
			}
			if(IdUtil.isNotEmpty(peopleId)){
				condition.and($.people.id.eq(peopleId));
			}
			if(StringUtil.isNotEmpty(workProgress)){
				condition.and($.workProgress.contains(workProgress));
			}
			if(StringUtil.isNotEmpty(workPlan)){
				condition.and($.workPlan.contains(workPlan));
			}
			if(StringUtil.isNotEmpty(issueDescription)){
				condition.and($.issueDescription.contains(issueDescription));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
	
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject);
			return projectReportDao.findAll(condition, pageRequest);
	}
	
	
	
	public ProjectReport getProjectReport(Long id){
		return projectReportDao.getOne(id);
	}
	
	public void deleteProjectReport(Long id){
		projectReportDao.delete(id);
	}

	public void saveProjectReport(ProjectReport projectReport) {
		projectReportDao.save(projectReport);
	}
	
	public Set<People> findPeoplesByProject(Project project){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($.project.id.eq(project.getId()));
		List<ProjectReport> projectResports = ListUtil.toList(this.projectReportDao.findAll(condition));
		Set<People> peoples = new HashSet<People>();
		for (ProjectReport projectReport : projectResports) {
			peoples.add(projectReport.getPeople());
		}
		return peoples;
	}

	public ProjectReport findOne(Long id) {
		return this.projectReportDao.findOne(id);
	}

}
		
